package com.module.overview.ui.widget

import android.animation.AnimatorSet
import android.animation.ValueAnimator
import android.content.Context
import android.util.AttributeSet
import android.view.animation.DecelerateInterpolator

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：12/6/2025
 *
 * 描述：
 *
 * 修订历史：
 *
 */
class AnimatedTripleRingView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : TripleRingView(context, attrs, defStyleAttr) {

    private var animatorSet: AnimatorSet? = null
    var animationDuration: Long = 1500
    private val animatedProgressValues = mutableListOf(0f, 0f, 0f)

    /**
     * 设置进度并播放动画
     * @param newProgressValues 新的进度值列表 (0-100)
     */
    fun setProgressWithAnimation(newProgressValues: List<Float>) {
        require(newProgressValues.size == 3) { "Must provide 3 progress values" }

        // 取消正在进行的动画
        cancelAnimation()

        val animators = mutableListOf<ValueAnimator>()

        for (i in 0..2) {
            val animator = ValueAnimator.ofFloat(animatedProgressValues[i], newProgressValues[i])
            animator.duration = animationDuration
            animator.interpolator = DecelerateInterpolator() // 使用与Flutter的easeOutQuart类似的插值器
            animator.addUpdateListener { animation ->
                animatedProgressValues[i] = animation.animatedValue as Float
                updateProgress()
            }
            animators.add(animator)
        }

        animatorSet = AnimatorSet().apply {
            playTogether(*animators.toTypedArray())
            addListener(object : android.animation.AnimatorListenerAdapter() {
                override fun onAnimationStart(animation: android.animation.Animator) {
                    isEnabled = false // 动画开始时禁用交互
                }

                override fun onAnimationEnd(animation: android.animation.Animator) {
                    isEnabled = true // 动画结束时启用交互
                }
            })
        }

        animatorSet?.start()
    }

    /**
     * 设置进度并从指定的起始值播放动画
     * @param oldProgressValues 起始进度值列表 (0-100)
     * @param newProgressValues 目标进度值列表 (0-100)
     */
    fun setProgressWithAnimation(oldProgressValues: List<Float>, newProgressValues: List<Float>) {
        require(oldProgressValues.size == 3 && newProgressValues.size == 3) { "Must provide 3 progress values for both old and new" }

        // 取消正在进行的动画
        cancelAnimation()

        val animators = mutableListOf<ValueAnimator>()

        for (i in 0..2) {
            animatedProgressValues[i] = oldProgressValues[i] // 设置起始值
            val animator = ValueAnimator.ofFloat(oldProgressValues[i], newProgressValues[i])
            animator.duration = animationDuration
            animator.interpolator = DecelerateInterpolator()
            animator.addUpdateListener { animation ->
                animatedProgressValues[i] = animation.animatedValue as Float
                updateProgress()
            }
            animators.add(animator)
        }

        animatorSet = AnimatorSet().apply {
            playTogether(*animators.toTypedArray())
        }

        animatorSet?.start()
    }

    private fun updateProgress() {
        progressValues = animatedProgressValues.map { it.toInt() }
        invalidate()
    }

    fun cancelAnimation() {
        animatorSet?.cancel()
        animatorSet = null
    }

    fun resetAnimation() {
        cancelAnimation()
        animatedProgressValues.fill(0f)
        updateProgress()
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        cancelAnimation()
    }
}